<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      margin: 0px;
      padding: 0px;
      background: url(./grid.png);
    }

    /* div的 */
    .blockBg {
      width: 300px;
      height: 300px;
      margin: 10px;
      background-color: black;
      border-radius: 50px;
    }
  </style>
</head>

<body>
  <div class="blockBg">
    <canvas id="canvas" width="300px" height="300px"></canvas>
  </div>
  <script>
    window.onload = () => {
      // 获取canvas上下文
      const canvas = document.getElementById('canvas')
      if (!canvas.getContext) return
      const ctx = canvas.getContext('2d')

      requestAnimationFrame(draw)

      function draw() {
        ctx.clearRect(0, 0, 300, 300)

        // 1.钟表圆盘
        drawClock()

        // 2.文本
        drawText()

        // 时间
        const date = new Date()
        const hours = date.getHours()
        const minutes = date.getMinutes()
        const seconds = date.getSeconds()

        // 3.时针
        drawHours(hours, minutes, seconds)

        // 4.分针
        drawMinutes(minutes, seconds)

        // 5.秒针
        drawSeconds(seconds)

        // 6.圆心
        drawCircle()

        // 7.刻度
        drawTicks()

        requestAnimationFrame(draw)
      }

      function drawClock() {
        ctx.save()
        ctx.translate(150, 150)
        ctx.beginPath()
        ctx.arc(0, 0, 130, 0, Math.PI * 2, false)
        ctx.closePath()
        ctx.fillStyle = 'white'
        ctx.fill()
        ctx.restore()
      }

      function drawText() {
        ctx.save()
        ctx.translate(150, 150)
        const texts = ['3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '1', '2']
        const R = 100
        ctx.fillStyle = 'black'
        ctx.font = '30px fangsong'
        ctx.textAlign = 'center'
        ctx.textBaseline = 'middle'
        texts?.forEach((num, index) => {
          const x = Math.cos(Math.PI / 6 * index) * R
          const y = Math.sin(Math.PI / 6 * index) * R
          ctx.fillText(num, x, y)
        })
        ctx.restore()
      }

      function drawHours(hours, minutes, seconds) {
        ctx.save()
        ctx.translate(150, 150)
        ctx.rotate(- Math.PI / 2)
        ctx.rotate(Math.PI / 6 * hours + Math.PI / 6 / 60 * minutes + Math.PI / 6 / 60 / 60 * seconds )
        ctx.beginPath()
        ctx.moveTo(0, 0)
        ctx.lineTo(50, 0)
        ctx.closePath()
        ctx.lineWidth = 5
        ctx.lineCap = 'round'
        ctx.strokeStyle = 'black'
        ctx.stroke()
        ctx.restore()
      }

      function drawMinutes(minutes, seconds) {
        ctx.save()
        ctx.translate(150, 150)
        ctx.rotate(- Math.PI / 2)
        ctx.rotate(Math.PI / 30 * minutes + Math.PI / 30 / 60 * seconds )
        ctx.beginPath()
        ctx.moveTo(0, 0)
        ctx.lineTo(70, 0)
        ctx.closePath()
        ctx.lineWidth = 2
        ctx.lineCap = 'round'
        ctx.strokeStyle = 'black'
        ctx.stroke()
        ctx.restore()
      }

      function drawSeconds(seconds) {
        ctx.save()
        ctx.translate(150, 150)
        ctx.rotate(- Math.PI / 2)
        ctx.rotate(Math.PI / 30 * seconds )
        ctx.beginPath()
        ctx.moveTo(0, 0)
        ctx.lineTo(80, 0)
        ctx.closePath()
        ctx.lineWidth = 1
        ctx.lineCap = 'round'
        ctx.strokeStyle = 'red'
        ctx.stroke()
        ctx.restore()
      }

      function drawCircle() {
        ctx.save()
        ctx.translate(150, 150)
        ctx.beginPath()
        ctx.arc(0, 0, 8, 0, Math.PI * 2, false)
        ctx.closePath()
        ctx.fill()
        ctx.beginPath()
        ctx.arc(0, 0, 5, 0, Math.PI * 2, false)
        ctx.closePath()
        ctx.fillStyle = 'gray'
        ctx.fill()
        ctx.restore()
      }

      function drawTicks() {
        ctx.save()
        ctx.translate(150, 150)
        // 时针的刻度
        ctx.save()
        for (let i = 0; i < 12; i++) {
          ctx.rotate(Math.PI / 6)
          ctx.beginPath()
          ctx.moveTo(0, -122)
          ctx.lineTo(0, -130)
          ctx.closePath()
          ctx.lineWidth = 3
          ctx.stroke()
        }
        ctx.restore()

        // 分针的刻度
        for (let i = 0; i < 60; i++) {
          ctx.save()
          ctx.rotate(Math.PI / 30 * i)
          ctx.beginPath()
          ctx.moveTo(0, -125)
          ctx.lineTo(0, -130)
          ctx.closePath()
          ctx.lineWidth = 1
          ctx.stroke()
          ctx.restore()
        }
        ctx.restore()
      }
    }
  </script>
</body>

</html>